package cc.wsyw126.java.lambda;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by junpeng.yang on 16/8/24.
 */
public class JavaFiltering {
    public static void main(String[] args) {
        // Create a List with String more than 2 characters
        List<String> strList = Arrays.asList("ni", "hao", "hello", "world");
        List<String> filtered = strList.stream().filter(x -> x.length() > 2)
                .collect(Collectors.toList());
        System.out.printf("Original List : %s, filtered list : %s %n", strList, filtered);


        System.out.println("-------------------");

        // Convert String to Uppercase and join them using coma
        List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.", "Canada");
        String G7Countries = G7.stream().map(x -> x.toUpperCase())
                .collect(Collectors.joining(", "));
        System.out.println(G7Countries);
        System.out.println("-------------------");

        // Create List of square of all distinct numbers
        List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
        List<Integer> distinct = numbers.stream().map(i -> i * i).distinct()
                .collect(Collectors.toList());
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, distinct);
        System.out.println("-------------------");
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, numbers.stream().distinct().collect(Collectors.toSet()));
        System.out.println("-------------------");
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, numbers.stream().distinct().collect(Collectors.toList()));
        System.out.println("-------------------");
        Collections.sort(numbers, ((o1, o2) -> o1 > o2 ? 1 : -1));
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, numbers);

        System.out.println("--------------");
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, Stream.of(new Integer[]{3,4,5,1,2,32,3,42,42,4,23,1,31,24,53,45,23,41,23,13,1}).sorted().collect(Collectors.toSet()));



    }
}